package com.totcy.atom.model;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.totcy.atom.CommonValues;
import com.totcy.atom.bean.DailySentenceCiBa;
import com.totcy.atom.net.HttpManager;
import com.totcy.atom.net.subscriber.BaseHttpSubscriber;
import com.totcy.atom.presenter.contract.DailySentenceContract;
import com.totcy.baselibrary.base.mvpbase.BaseModelImpl;
import com.totcy.baselibrary.httpcache.ACache;
import com.totcy.baselibrary.utils.DateUtils;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * description : 每日一句model层
 *
 * @author : Totcy
 * @date : 2017/12/7 17:05
 * @update : 2017/12/7
 */

public class DailySentenceModel extends BaseModelImpl implements DailySentenceContract.Model {

    private ACache aCache;
    private Gson gson = new Gson();
    private BaseHttpSubscriber<DailySentenceCiBa> subscriber;


    public DailySentenceModel(Context context) {
        super(context);
        aCache = ACache.get(context);
    }

    /**
     * 取消model层网络请求
     */
    @Override
    public void canCelRequest() {
        if (subscriber != null) {
            subscriber.unSubscribe();
        }
    }

    /**
     * 从网络获取数据
     */
    @Override
    public void getDailySentence(final DataCallback callback) {
        //读取本地缓存 根据年月日 作为key
        String cacheJson = aCache.getAsString(CommonValues.DAILY_SENTENCE_CIBA
                + DateUtils.getStrTime(System.currentTimeMillis(),DateUtils.YYYY_MM_DD));
        if(!TextUtils.isEmpty(cacheJson)){
            DailySentenceCiBa bean = gson.fromJson(cacheJson,DailySentenceCiBa.class);
            callback.onDailySentenceCiBa(bean);
            return;
        }

        /*参数 ：params.put("date","2017-12-03"); //params.put("type","last"); //params.put("type","next");*/
        HashMap<String, Object> params = new HashMap<>();
        HttpManager.getApiService(mContext)
                .getDailySentence(CommonValues.DAILY_SENTENCE_CIBA, params)
                //线程调度
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                //结果处理
                .subscribe(subscriber = new BaseHttpSubscriber<DailySentenceCiBa>() {
                    @Override
                    public void onSuccess(DailySentenceCiBa bean) {
                        aCache.put(CommonValues.DAILY_SENTENCE_CIBA
                                        + DateUtils.getStrTime(System.currentTimeMillis(),DateUtils.YYYY_MM_DD)
                                ,gson.toJson(bean)
                                ,ACache.TIME_DAY
                        );
                        callback.onDailySentenceCiBa(bean);
                    }

                    @Override
                    public void onError(int code, String errorMsg) {
                        //
                        callback.onDataNotAvailable(code, errorMsg);
                    }
                });
    }
}
